<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController; 
/**
 * 首页
 */
class IndexController extends HomebaseController {
	protected $posts_obj;
	protected $users_obj;

	function _initialize(){
		parent::_initialize();
		$this->posts_obj = M("posts","cmf_");
		$this->users_obj = M("users","cmf_");

	}
    //首页 评选人物列表
	public function index() {
		$info = $this->posts_obj->order('post_like desc')->select();
        $right =$this->posts_obj->order('post_date desc')->limit(3)->select();
		$this->assign("info",$info);
        $this->assign("right",$right);
    	$this->display(":index");
    }

    /*留言
	// 输入：$message
	// 输出：无
    */
    public function approval(){
    	$message = I("param.message");
    	$ip = get_client_ip();
    	$userid = $this->users_obj->field("id")->where("last_login_ip =".$ip)->find();
    	$data['user_id'] = $userid;
    	$data['message'] = $message;
    	$map = $this->user_obj->add($data);
    	if($map){
    		$this->success("留言成功!",U("Index/index"));
    	}
    	else{
    		$this->success("留言失败!");
    	}
    }

    public function detail(){
        if(isset($_GET['id']))
            $id=intval(I('get.id'));
        $data=$this->posts_obj->where('id='.$id)->find();
        $this->assign('data',$data);
        $this->display(":detail");
    }

    public function like(){
        $id = I('get.id');
        $status = I('get.state');
        if($id != '' && $status != ''){
            $obj = D('posts');
            $data = $obj -> where("id = $id and post_exist = 1") -> find();
            $like = $data['post_like'];
            if($status == 0){
                $like++;
                $obj -> where("id = $id and post_exist = 1") -> save("post_like = $like");
            }elseif($status == 1){
                $like--;
                $obj -> where("id = $id and post_exist = 1") -> save("post_like = $like");
            }
        }
    }
}


